CXX=g++
CXXFLAGS=-Wall -Wextra -Winit-self -Wstrict-aliasing=2 -Wfloat-equal -Wno-comment -Wno-missing-field-initializers -Wno-unused -DPTRACING=1 -D_REENTRANT -D_GNU_SOURCE=1 -felide-constructors -Wreorder -fPIC -I../common/include -Iinclude -Iinclude/file -I../embedding/include
LDFLAGS=-lpthread
LIBS=-L../common/build-$(ARCH) -lcommon
CXXFLAGS_ARCH=
LDFLAGS_ARCH=
ifeq ($(DEBUG_BUILD),yes)
CXXFLAGS += -ggdb -O0 -DDEBUG
endif # DEBUG_BUILD
CXXFLAGS += $(CXXFLAGS_ARCH)
LDFLAGS += $(LDFLAGS_ARCH)
ifndef $(ARCH)
ARCH = default
endif

RM=rm
RMFLAGS=-rf
MKDIR=mkdir
AR=ar
ARFLAGS=rcs

SOURCE_DIR=src
FILE_SOURCE_DIR=$(SOURCE_DIR)/file

BUILD_DIR=build-$(ARCH)
FILE_BUILD_DIR=$(BUILD_DIR)/file

SOURCE_FILES=$(shell cd $(SOURCE_DIR) ; ls *.cxx)
FILE_SOURCE_FILES=$(shell cd $(FILE_SOURCE_DIR) ; ls *.cxx)

LIBOUT=$(BUILD_DIR)/libprotocols.a

OBJ=$(SOURCE_FILES:%.cxx=$(BUILD_DIR)/%.o)
FILE_OBJ=$(FILE_SOURCE_FILES:%.cxx=$(FILE_BUILD_DIR)/%.o)

default: debug

release: all

debug:
	$(MAKE) DEBUG_BUILD=yes all
	
all: $(LIBOUT)

$(LIBOUT): init $(OBJ) $(FILE_OBJ)
	$(AR) $(ARFLAGS) $(LIBOUT) $(OBJ) $(FILE_OBJ)

$(BUILD_DIR)/%.o: $(SOURCE_DIR)/%.cxx
	$(CXX) -c $(CXXFLAGS) -o $@ $<

$(FILE_BUILD_DIR)/%.o: $(FILE_SOURCE_DIR)/%.cxx
	$(CXX) -c $(CXXFLAGS) -o $@ $<

init:
	@if [ ! -d $(BUILD_DIR) ] ; then $(MKDIR) -p $(BUILD_DIR) ; fi
	@if [ ! -d $(FILE_BUILD_DIR) ] ; then $(MKDIR) -p $(FILE_BUILD_DIR) ; fi

.PHONY: clean

clean:
	$(RM) $(RMFLAGS) $(BUILD_DIR) $(FILE_BUILD_DIR)

